package project.pianpian.controller;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import project.pianpian.entity.Tag;
import project.pianpian.entity.User;
import project.pianpian.entity.Video;
import project.pianpian.mapper.TagMapper;
import project.pianpian.mapper.UserMapper;
import project.pianpian.mapper.VideoMapper;
import project.pianpian.utils.ReturnUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * description:
 * author :万霁初
 * date :2023/1/2 22:48
 **/
@RestController
@Slf4j
@RequestMapping("/home")
public class HomePage {
    @Autowired
    UserMapper userMapper;
    @Autowired
    VideoMapper videoMapper;
    @Autowired
    TagMapper tagMapper;

    @GetMapping("/video")
    public JSONObject selfVideo(@RequestParam("id") int id){
        User user = userMapper.selectById(id);

        Map map=new HashMap();
        map.put("uploader_id",user.getId());
        List<Video> videos = videoMapper.selectByMap(map);

        if (videos.size()==0){
            return ReturnUtils.success(200,"请求成功，但该用户未上传视频");
        }

        List<List<Tag>> tagsList=new ArrayList<>();
        for (Video video : videos) {
            Map map1=new HashMap();
            map1.put("video_id",video.getId());
            List<Tag> tags = tagMapper.selectByMap(map1);
            tagsList.add(tags);
        }

        JSONObject returnJSON=new JSONObject();
        returnJSON.put("user",user);
        returnJSON.put("videos",videos);
        returnJSON.put("tagsList",tagsList);

        return ReturnUtils.success(200,"获取用户上传视频成功",returnJSON);
    }
}
